require 'test/unit'
require '../weather.rb'

class WeatherTest < Test::Unit::TestCase

  def setup
    @weather = Weather.new('http://xml.weather.yahoo.com/forecastrss?p=USCA0987&u=f')
  end

  def test_conditions
    assert_kind_of String, @weather.condition
  end

  def test_photo
    assert File.exists?(@weather.photo)
  end

  def test_cache_should_not_recheck_before_15_minutes
    @weather.condition

    @weather.instance_variable_get("@cacheable_caches")['feed'][:cached_at] = Time.now - 14.minutes

    @weather.condition
    assert_in_delta @weather.instance_variable_get("@cacheable_caches")['feed'][:cached_at], Time.now - 14.minutes, 1
  end

  def test_cache_should_revalidate_after_15_minutes
    @weather.condition

    @weather.instance_variable_get("@cacheable_caches")['feed'][:cached_at] = (Time.now - 15.minutes) - 1
    @weather.instance_variable_get("@cacheable_caches")['condition'][:cached_at] = (Time.now - 15.minutes) - 1

    @weather.condition
    assert_in_delta @weather.instance_variable_get("@cacheable_caches")['feed'][:cached_at], Time.now, 1
  end

end
